package com.codeoss.module.uom.service.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.module.product.model.ProductProduct;
import com.codeoss.module.product.service.IProductProductService;
import com.codeoss.module.uom.dao.UomUomConversionMapper;
import com.codeoss.module.uom.model.UomUomConversion;
import com.codeoss.module.uom.service.IUomUomConversionService;

@Service
public class UomUomConversionService extends BaseService<UomUomConversionMapper, UomUomConversion>
		implements IUomUomConversionService {

	@Autowired
	private IProductProductService productProductService;
	@Override
	public BigDecimal convert(Long productProductId, Long uomUomToId, BigDecimal qty) {
		// TODO Auto-generated method stub
		ProductProduct productProduct=productProductService.getById(productProductId);
		Long uomUomId=productProduct.getUomUomId();
		if(uomUomToId.longValue()==uomUomId.longValue()) {
			return qty;
		}
		QueryWrapper<UomUomConversion> query=new QueryWrapper<UomUomConversion>();
		query.eq("product_product_id", productProductId);
		query.eq("uom_uom_to_id", uomUomToId);
		query.eq("uom_uom_id", uomUomId);
		UomUomConversion uomUomConversion=getOne(query);
		BigDecimal rate=uomUomConversion.getRate();
		BigDecimal uomQty=rate.multiply(qty);
		return uomQty;
	}
}